/**
 * Copyright &copy; 2013-2015 山东易科德软件有限公司   All rights reserved.
 */
package cn.net.ecode.modules.sys.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.net.ecode.common.config.Global;
import cn.net.ecode.common.persistence.Page;
import cn.net.ecode.common.service.CrudService;
import cn.net.ecode.common.service.ServiceException;
import cn.net.ecode.common.utils.StringUtils;
import cn.net.ecode.modules.sys.dao.EmpUserDao;
import cn.net.ecode.modules.sys.entity.Employee;
import cn.net.ecode.modules.sys.entity.Office;
import cn.net.ecode.modules.sys.entity.User;
import cn.net.ecode.modules.sys.utils.DataScopeUtils;
import cn.net.ecode.modules.sys.utils.UserUtils;

/**
 * 员工管理Service
 * 
 * @author ThinkGem
 * @version 2015-4-24
 */
@Service
@Transactional(readOnly = true)
public class EmpUserService extends CrudService<EmpUserDao, Employee> {

	@Autowired
	private EmployeeService employeeService;
	@Autowired
	private UserService userService;

	public Employee get(String empCode) {
		throw new ServiceException("方法未实现，请调用getByUserCode方法。");
	}

	public Employee getByUserCode(String userCode) {
		Employee employee =new Employee();
		employee.setUserCode(userCode);
		return dao.getByUserCode(employee);
	}

	public Employee getByUserCode(String userCode, boolean isNewRecord) {
		Employee entity = null;
		if (StringUtils.isNotBlank(userCode)) {
			entity = getByUserCode(userCode);
			if (isNewRecord && entity != null) {
				throw getValidationIdExistsException();
			}
		}
		if (entity == null) {
			try {
				entity = new Employee();
			} catch (Exception e) {
				throw new ServiceException(e);
			}
		}
		return entity;
	}

	public List<Employee> findList(Employee employee) {
		// 生成数据权限过滤条件
		DataScopeUtils.genSql(employee, "dsf", "company_m,office_m", "c,o");
		return super.findList(employee);
	}

	public Page<Employee> findPage(Page<Employee> page, Employee employee) {
		// 生成数据权限过滤条件
		DataScopeUtils.genSql(employee, "dsf", "company_m,office_m", "c,o");
		return super.findPage(page, employee);
	}

	@Transactional(readOnly = false)
	public void save(Employee employee) {
		User user = employee.getLoginInfo();
		Employee oldEmp=employeeService.get(employee);
		// 保存员工信息
		if (oldEmp==null) {
			employee.setEmpNo(user.getLoginCode()); // 集团唯一

			employee.setIsLoginUser(Global.NO); // 不通过EmployeeService保存登录信息
			Office office = new Office();
			office.setOfficeCode(employee.getOfficeCode());
			office.setOfficeName(employee.getOfficeName());
			employee.setOffice(office);
			employee.setCreateBy(UserUtils.getUser());
			employee.setUpdateBy(UserUtils.getUser());
			employee.setCreateDate(new Date());
			employee.setUpdateDate(new Date());
			employeeService.insertEmp(employee);
		}
		

		// 保存用户登录信息
		user.setRefCode(employee.getEmpCode()); // 关联用户编码
		user.setRefName(employee.getEmpName()); // 关联用户名称
		user.setMgrType(User.MGR_TYPE_NOT_ADMIN); // 非管理员
		user.setMobile(employee.getMobile());
		user.setPhone(employee.getOfficePhone());
		user.setEmail(employee.getEmail());
		user.setIsNewRecord(employee.getIsNewRecord());
		user.setOfficeCode(employee.getOfficeCode());
		user.setOfficeName(employee.getOfficeName());
		user.setUserCode(employee.getEmpCode());
		user.setUserName(employee.getEmpName());
		user.setLoginCode(employee.getEmpCode());
		userService.save(user);

	}

	/**
	 * 删除用户
	 * 
	 * @param employee
	 * @return
	 */
	@Transactional(readOnly = false)
	public void delete(Employee employee) {
		employeeService.delete(employee);
		userService.delete(employee.getLoginInfo());
	}

	/**
	 * 停用/离职用户
	 * 
	 * @param employee
	 * @return
	 */
	@Transactional(readOnly = false)
	public int disableUser(Employee employee) {
		employeeService.leaveEmployee(employee);
		return userService.disableUser(employee.getLoginInfo());
	}

	/**
	 * 启用/解冻用户
	 * 
	 * @param employee
	 * @return
	 */
	@Transactional(readOnly = false)
	public int activateUser(Employee employee) {
		employeeService.activateEmployee(employee);
		return userService.activateUser(employee.getLoginInfo());
	}

}